/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Practica1Frame.java
 *
 * Created on 15-mar-2011, 17:23:07
 */

package pr1.gui;

import java.awt.BorderLayout;

import javax.swing.DefaultComboBoxModel;

import org.math.plot.Plot2DPanel;

import pr1.cromosoma.CromosomaF1;
import pr1.cromosoma.CromosomaF2;
import pr1.cromosoma.CromosomaF3;
import pr1.cromosoma.CromosomaF4;
import pr1.cromosoma.CromosomaF5;
import pr1.cromosoma.CromosomaFactory;
import pr1.cruce.CruceSimple;
import pr1.seleccion.Ruleta;
import pr1.seleccion.Torneo;

/**
 *
 * @author Gonzalo Ortiz Jaureguizar (gortiz at software.imdea.org)
 */
public class Practica1Frame extends javax.swing.JFrame {

    public static final int RULETA = 0;
    public static final int TORNEO = 1;

    double precision = 0.001;
    Plot2DPanel plot;
    AGRunner runner;

    DefaultComboBoxModel seleccionModel;
    DefaultComboBoxModel funcionModel;

    /** Creates new form Practica1Frame */
    public Practica1Frame() {
        seleccionModel = new DefaultComboBoxModel();
        seleccionModel.addElement("Ruleta");
        seleccionModel.addElement("Torneo");

        funcionModel = new DefaultComboBoxModel();
        funcionModel.addElement("F1");
        funcionModel.addElement("F2");
        funcionModel.addElement("F3");
        funcionModel.addElement("F4");
        funcionModel.addElement("F5");

        initComponents();

        plot = new Plot2DPanel();
        plot.setLegendOrientation("SOUTH");
        runner = new AGRunner();

        plotPanel.add(plot, BorderLayout.CENTER);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jSplitPane1 = new javax.swing.JSplitPane();
        jPanel1 = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        calcularButton = new javax.swing.JButton();
        jPanel4 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        generacionesSpinner = new javax.swing.JSpinner();
        jLabel8 = new javax.swing.JLabel();
        tamPobSpinner = new javax.swing.JSpinner();
        jLabel6 = new javax.swing.JLabel();
        cruceSpinner = new javax.swing.JSpinner();
        jLabel5 = new javax.swing.JLabel();
        mutacionSpinner = new javax.swing.JSpinner();
        jLabel4 = new javax.swing.JLabel();
        precisionTextField = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        seleccionComboBox = new javax.swing.JComboBox();
        jLabel9 = new javax.swing.JLabel();
        nSpinner = new javax.swing.JSpinner();
        jLabel7 = new javax.swing.JLabel();
        elitismoSpinner = new javax.swing.JSpinner();
        jLabel2 = new javax.swing.JLabel();
        funcionComboBox = new javax.swing.JComboBox();
        plotPanel = new javax.swing.JPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.setLayout(new java.awt.BorderLayout());

        jPanel3.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));

        calcularButton.setText("Recalcular");
        calcularButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                calcularButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addContainerGap(330, Short.MAX_VALUE)
                .addComponent(calcularButton)
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(calcularButton)
                .addContainerGap())
        );

        jPanel1.add(jPanel3, java.awt.BorderLayout.PAGE_END);

        jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder("Configuracion"));
        jPanel4.setLayout(new java.awt.GridLayout(9, 2, 3, 3));

        jLabel1.setText("Generaciones");
        jPanel4.add(jLabel1);

        generacionesSpinner.setModel(new javax.swing.SpinnerNumberModel(Integer.valueOf(100), Integer.valueOf(0), null, Integer.valueOf(1)));
        jPanel4.add(generacionesSpinner);

        jLabel8.setText("Tamaño pob");
        jPanel4.add(jLabel8);

        tamPobSpinner.setModel(new javax.swing.SpinnerNumberModel(Integer.valueOf(100), Integer.valueOf(0), null, Integer.valueOf(1)));
        jPanel4.add(tamPobSpinner);

        jLabel6.setText("Prob cruce (%)");
        jPanel4.add(jLabel6);

        cruceSpinner.setModel(new javax.swing.SpinnerNumberModel(2, 0, 100, 1));
        jPanel4.add(cruceSpinner);

        jLabel5.setText("Prob mutación (%)");
        jPanel4.add(jLabel5);

        mutacionSpinner.setModel(new javax.swing.SpinnerNumberModel(2, 0, 100, 1));
        jPanel4.add(mutacionSpinner);

        jLabel4.setText("Precisión");
        jPanel4.add(jLabel4);

        precisionTextField.setText("0.001");
        precisionTextField.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                precisionTextFieldFocusLost(evt);
            }
        });
        jPanel4.add(precisionTextField);

        jLabel3.setText("Selección");
        jPanel4.add(jLabel3);

        seleccionComboBox.setModel(seleccionModel);
        jPanel4.add(seleccionComboBox);

        jLabel9.setText("Valor de n");
        jPanel4.add(jLabel9);

        nSpinner.setModel(new javax.swing.SpinnerNumberModel(Integer.valueOf(1), Integer.valueOf(1), null, Integer.valueOf(1)));
        jPanel4.add(nSpinner);

        jLabel7.setText("Elitismo");
        jPanel4.add(jLabel7);

        elitismoSpinner.setModel(new javax.swing.SpinnerNumberModel(2, 0, 100, 1));
        jPanel4.add(elitismoSpinner);

        jLabel2.setText("Función");
        jPanel4.add(jLabel2);

        funcionComboBox.setModel(funcionModel);
        jPanel4.add(funcionComboBox);

        jPanel1.add(jPanel4, java.awt.BorderLayout.CENTER);

        jSplitPane1.setLeftComponent(jPanel1);

        plotPanel.setLayout(new java.awt.BorderLayout());
        jSplitPane1.setRightComponent(plotPanel);

        getContentPane().add(jSplitPane1, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void calcularButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_calcularButtonActionPerformed

        CromosomaFactory factory;
        boolean maximizar;

        switch(funcionComboBox.getSelectedIndex()) {
            case 0:
                factory = new CromosomaF1.Factory();
                maximizar = true;
                break;
            case 1:
                factory = new CromosomaF2.Factory();
                maximizar = true;
                break;
            case 2:
                factory = new CromosomaF3.Factory();
                maximizar = false;
                break;
            case 3:
                factory = new CromosomaF4.Factory();
                ((CromosomaF4.Factory)factory).setN((Integer)nSpinner.getValue());
                maximizar = false;
                break;
            case 4:
                factory = new CromosomaF5.Factory();
                maximizar = false;
                break;
            default:
                return ;
        }

        runner.setFactory(factory);
        runner.setCruce(new CruceSimple(factory));
        runner.setGeneraciones((Integer)generacionesSpinner.getValue());
        runner.setPrecision(precision);
        runner.setProbCruce((Integer)cruceSpinner.getValue());
        runner.setProbMutacion((Integer)mutacionSpinner.getValue());
        runner.setTamPoblacion((Integer)tamPobSpinner.getValue());
        runner.setElitismo((Integer)elitismoSpinner.getValue());
        switch(seleccionComboBox.getSelectedIndex()) {
            case RULETA:
                runner.setSeleccion(new Ruleta());
                break;
            case TORNEO:
                runner.setSeleccion(new Torneo());
                break;
        }
        
        runner.plot(plot, maximizar);

        
    }//GEN-LAST:event_calcularButtonActionPerformed

    private void precisionTextFieldFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_precisionTextFieldFocusLost
        try {
            precision = Double.valueOf(precisionTextField.getText());
        }
        catch (Exception e) {
            precisionTextField.setText(Double.toString(precision));
        }
    }//GEN-LAST:event_precisionTextFieldFocusLost

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {

        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Practica1Frame().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton calcularButton;
    private javax.swing.JSpinner cruceSpinner;
    private javax.swing.JSpinner elitismoSpinner;
    private javax.swing.JComboBox funcionComboBox;
    private javax.swing.JSpinner generacionesSpinner;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JSplitPane jSplitPane1;
    private javax.swing.JSpinner mutacionSpinner;
    private javax.swing.JSpinner nSpinner;
    private javax.swing.JPanel plotPanel;
    private javax.swing.JTextField precisionTextField;
    private javax.swing.JComboBox seleccionComboBox;
    private javax.swing.JSpinner tamPobSpinner;
    // End of variables declaration//GEN-END:variables

}
